const fs=require("fs")
const path=require("path")

let getMimeJson = function (name) {
    name=name.slice(name.indexOf('.')+1)
    let result=fs.readFileSync('./static/json/mine.json')
    return JSON.parse(result.toString())[name]
}

exports.static=function(req,res,static){
     // 1.获取地址
    let pathname=new URL(req.url,"http://localhost:3000").pathname;
    // 默认/index.html
    pathname=pathname=='/'?'/index.html':pathname;
    // 获取后缀
    let extname=path.extname(pathname);
    if(pathname!='/favicon.ico'){
        try{
            let data=fs.readFileSync(static+pathname)
            res.writeHead(200,{'Content-Type':getMimeJson(extname)+";charset=UTF8"})
            res.end(data)
        }catch(err){
            console.log(err.toString())
        }
    }
}

